package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.bundle.dto.BundleCreateDTO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.dingtalk.pojo.dto.BundleDetailDTO;
import com.xbongbong.pro.dingtalk.pojo.dto.BundleListDTO;
import com.xbongbong.pro.dingtalk.pojo.vo.BundleDetailHeaderVO;
import com.xbongbong.pro.dingtalk.pojo.vo.BundleDetailVO;
import com.xbongbong.pro.dingtalk.pojo.vo.BundleListVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.service.BundleService;
import com.xbongbong.saas.service.DingBundleService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 应用模块名称<p>
 * 代码描述<p>
 * Company: 逍邦网络科技有限公司<p>
 *
 * @author chenshan
 * @version v1.0
 * @since 2020/9/1 9:01 下午
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/bundle")
public class MobileDingBundleController {
    @Resource
    private DingBundleService dingBundleService;
    @Resource
    private BundleService bundleService;
    
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid BundleListDTO bundleListDTO, BindingResult br) {
        XbbResponse<BundleListVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BundleListVO bundleListVO = dingBundleService.list(bundleListDTO);
                response = new XbbResponse<>(bundleListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    
    @RequestMapping(value = "/getDetail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid BundleDetailDTO bundleDetailDTO, BindingResult br) {
        XbbResponse<BundleDetailHeaderVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BundleDetailHeaderVO bundleDetailHeaderVO = dingBundleService.getHeader(bundleDetailDTO);
                response = new XbbResponse<>(bundleDetailHeaderVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    
    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid BundleDetailDTO bundleDetailDTO, BindingResult br) {
        XbbResponse<BundleDetailVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BundleDetailVO bundleDetailVO = dingBundleService.get(bundleDetailDTO);
                response = new XbbResponse<>(bundleDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String createBundle(@RequestBody @Valid BundleCreateDTO bundleCreateDTO, BindingResult br) {
        XbbResponse<BaseVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = bundleService.createBundle(bundleCreateDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    
}
